// Created by carolsail

import { ENUM_AUDIO_CLIP, ENUM_GAME_STATUS, ENUM_UI_TYPE } from "../Enum";
import { StaticInstance } from './../StaticInstance';
import AudioManager from "../manager/AudioManager";
import BaseLayer from "./Baselayer";
import DataManager from "../manager/DataManager";
import SdkManager from "../manager/SdkManager";
import ToastManager from "../manager/ToastManager";
import { ICallBack, IRoom } from "../shared/common/types";
import { uuid } from "../Utils";

const { ccclass, property } = cc._decorator;

@ccclass
export default class ShareLayer extends BaseLayer {

    panel: cc.Node = null
    btnAgree: cc.Node = null
    btnClose: cc.Node = null

    onLoad() {
        this.panel = cc.find('style/panel', this.node)
        this.btnAgree = cc.find('btn_agree', this.panel)
        this.btnClose = cc.find('btn_close', this.panel)
        this.btnAgree.on('click', this.onAgreeClick, this)
        this.btnClose.on('click', this.onCloseClick, this)
    }

    onDestroy() {
        this.btnAgree.off('click', this.onAgreeClick, this)
        this.btnClose.off('click', this.onCloseClick, this)
    }

    onEnable() {
        this.zoomIn(this.panel)
        SdkManager.instance.getLoginInfo(info => {
            // 授权登录
            if (info.status) {
                SdkManager.instance.getLoginCode(async obj => {
                    const data = await DataManager.instance.wsClient.callApi('Login', { code: obj.data })
                    if (!data.isSucc) {
                        ToastManager.instance.show('授权失败：服务器异常', { gravity: 'TOP', bg_color: cc.color(226, 69, 109, 255) })
                        StaticInstance.uiManager.toggle(ENUM_UI_TYPE.SHARE, false)
                        return
                    }
                    // 授权登录成功，缓存数据
                    DataManager.instance.user = info.data
                    DataManager.instance.user.openid = data.res.openid
                    DataManager.instance.save()
                })
            } else {
                ToastManager.instance.show('授权失败：拒绝授权', { gravity: 'TOP', bg_color: cc.color(226, 69, 109, 255) })
                StaticInstance.uiManager.toggle(ENUM_UI_TYPE.SHARE, false)
            }
        })
    }

    onDisable() { }

    onAgreeClick() {
        AudioManager.instance.playSound(ENUM_AUDIO_CLIP.CLICK)
        DataManager.instance.wsClient.callApi('Connect', { user: DataManager.instance.user }).then(async () => {
            const data = await DataManager.instance.wsClient.callApi('RoomJoin', { roomId: DataManager.instance.shareRoomId, player: DataManager.instance.user })
            if (!data.isSucc) {
                ToastManager.instance.show(data.err.message, { gravity: 'TOP', bg_color: cc.color(226, 69, 109, 255) })
                return
            }
            DataManager.instance.room = data.res.room
            const types = StaticInstance.uiManager.getActiveTypes()
            for (let i = 0; i < types.length; i++) {
                StaticInstance.uiManager.toggle(types[i], false)
            }
            StaticInstance.uiManager.toggle(ENUM_UI_TYPE.ROOM)
        })
    }

    onCloseClick() {
        AudioManager.instance.playSound(ENUM_AUDIO_CLIP.CLICK)
        StaticInstance.uiManager.toggle(ENUM_UI_TYPE.SHARE, false)
    }
}
